'''
Created on Dec 6, 2009

@author: tliu
'''

import cgi
import hashlib
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from error import UserError
from model.User import getUserByUsername
import appengine_utilities.sessions
import os
from util import AuthorizedPage
from model.Project import getProjectByName
from model.Entry import createEntry


class PostEntryPage(AuthorizedPage):
    destination = ""
    content = ""
    type = ""
    title = ""
    def getIfAuthorized(self):        
        project_list = []
        for project in self.authorizedUser.getProjects():
            dict = {}
            if project.name == self.destination:
                dict["selected"] = True
            dict["name"] = project.name
            project_list.append(dict)
            
        errors = {'missingField' : "Please fill out all of the fields!",
                  'badProject' : "Cannot update project!"}
        
        if self.error in errors.keys():
            error = errors[self.error]
        else:
            error = ""
            
        question_checked = ""
        idea_checked = ""
        if self.type == "question":
            question_checked = "checked"
        if self.type == "idea":
            idea_checked = "checked"
        self.outputHeader()
        templatePath = os.path.join(os.path.dirname(__file__), 'post_entry.html')    
        self.response.out.write(template.render(templatePath, {"projects" : project_list,
                                                               "destination" : self.destination,
                                                               "content" : self.content,
                                                               "question_checked" : question_checked,
                                                               "idea_checked" : idea_checked,
                                                               "title" : self.title,
                                                               "error" : error})) 
    
    def postIfAuthorized(self):
        self.destination = cgi.escape(self.request.get("destination"))
        self.content = cgi.escape(self.request.get("content"))
        self.type = self.request.get("type")
        self.title = cgi.escape(self.request.get("title"))
        
        if self.type not in ['question', 'idea'] or self.destination == "" or self.content == "" or self.title == "":
            self.error = 'missingField'
            self.getIfAuthorized()
            return
        
        entry = createEntry(self.authorizedUser, self.title, self.type, self.content)
        
        if self.destination == "1":
            self.authorizedUser.addBlogEntry(entry)
            self.redirect("/profile?user=%s" % self.authorizedUser.username)
            return
            
        else:
            destination = getProjectByName(self.destination)
            if destination.owner.username != self.authorizedUser.username:
                self.error = 'badProject'
                self.getIfAuthorized()
                return
            destination.addEntry(entry)
            self.redirect(destination.url)
            return
        
        
        
        
        
